import rssJson from "../config/rss.json"

let spide = require("rssspider")
const { success, fail } = require("../lib/result")

exports.get = async () => {
  try {
    let channels = rssJson.channel
    async function fetchData(feed, title) {
      return await spide.fetchRss(feed).then(data => {
        return data
      })
    }
    // let rees = await fetchData(channels[0].link, channels[0].title);
    let rees = async () => {
      let newData = await channels.forEach(async (e, i) => {
        if (e.work != false) {
          return await fetchData(e.link, e.title)
        }
      })
      console.log(newData)

      return newData
    }

    return success(await rees())
  } catch (error) {
    return fail(1, JSON.stringify(error))
  }
}
